import { defineNuxtPlugin } from "nuxt/app";
// import MobileDetect from "mobile-detect";
import { judgeBrand, arrModal, getSystemVersion } from "~/utils/deviceType";
// import { useGlobalStore } from "~~/store/global";
// import { publicIpv4 } from "public-ip"; //  "public-ip": "^6.0.1",

const onMobilePhone = async () => {
  let ua = navigator.userAgent;
  // console.log(ua, "获取ua信息");

  let md = new MobileDetect(ua);
  let os = md.os(); //获取系统
  let HMS;
  // 获取华为
  if (ua.split(")")[0].includes("HMSCore")) {
    HMS = ua.split(")")[0].split("HMSCore")[0].split(";");
  }
  let model = HMS ? HMS[HMS.length - 2] : "";
  let val;
  if (os == "iOS") {
    //ios系统的处理
    // @ts-ignore
    model = md.mobile();
    // console.log(md.mobile(), os, '获取系统', os == 'iOS');
    if (model == "iPhone") {
      // 第二个参数,暂时没用到
      let iphoneArr = [
        ["430,932,3", "A15", "iPhone 15Plus || 15/14 Pro Max"],
        ["428,926,3", "A14", "iPhone 14Plus || 12/13 Pro Max"],
        ["393,852,3", "A15", "iPhone 15 || 15(14) Pro"],
        ["390,844,3", "A14", "iPhone 12/12 Pro || 13/13 Pro || 14"],
        ["375,812,3", "A15", "iPhone 13(12) min"],
        ["414,896,3", "A13", "iPhone 11 Pro Max(Xs Max)"],
        ["375,812,3", "A13", "iPhone 11 Pro(X/Xs)"],
        ["414,896,2", "A13", "iPhone 11(XR)"],
        ["414,736,3", "A12", "iPhone 8(7/6s) Plus"],
        ["375,667,2", "A11", "iPhone 8(7/6)"],
        ["320,568,2", "A11", "iPhone 5(s/se)"],
        ["320,480,2", "A10", "iPhone 4(s)"],
      ];

      // 获取GPU信息
      // let canvas = document.createElement("canvas"),
      // gl = canvas.getContext("experimental-webgl"),
      // @ts-ignore
      // debugInfo = gl.getExtension("WEBGL_debug_renderer_info");
      // @ts-ignore

      // let iphoneGPU = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
      let width = window.screen.width;
      let height = window.screen.height;
      let devicePixelRatio = window.devicePixelRatio;
      // console.log(width, height, devicePixelRatio);

      let baseInfo = width + "," + height + "," + devicePixelRatio;
      iphoneArr.some((item) => {
        if (item[0] === baseInfo) {
          model = item[2];
          // console.log("iphoneGPU", iphoneGPU == item[1]);
        }
      });
      // let os = +md.version("iPhone");
      // console.log(`${md.mobile()} IOS${os}`);
      // console.log(model, '鸡鸡', ua.toLowerCase());
      val = model;
    } else {
      model = "iPad";
      val = "iPad";
    }
  } else if (os == "AndroidOS") {
    //Android系统的处理
    let j;
    let sss = ua.split(";");
    for (let i = 0; i < sss.length; i++) {
      if (sss[i].indexOf("Build/") > 0) {
        j = i;
        break;
      }
    }
    // @ts-ignore
    if (j > -1) {
      // @ts-ignore
      model = sss[j].substring(0, sss[j].indexOf("Build/"));
    }
    const bdxh = judgeBrand(ua.toLowerCase());
    // console.log("比对型号:", bdxh);

    arrModal.map((item) => {
      if (item.type == bdxh) {
        item.modelNumber.map((vm) => {
          if (vm.lable == model.trim()) {
            val = vm.value;
          }
        });
      }
    });
    val = val ? val : "Android";
  }
  // console.log(val);
  val = val ? val : "未知型号";
  // localStorage.setItem("model", val);
  console.log("设备类型:", val);
  var systemVersion = getSystemVersion(ua);
  // console.log("系统版本号为：", systemVersion);
  return val + ` version:${systemVersion ? systemVersion : ""}`;
};


export default defineNuxtPlugin((nuxtApp) => {
  // const globalStore = useGlobalStore();
  //  1. 获取设备型号
  // onMobilePhone().then((res) => {
  //  globalStore.$patch((state) => {
  //     state.diviceType = res;
  //   });
  // });
  
});
